package com.yyb.gui;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Yamos
 * @description Main
 * @date 2021/1/14 0014 15:04
 */
public class BaseMain {
    public static void main(String[] args) {
        System.out.println("THis is java GUI base");

        // 边界布局管理器,东西南北中 布局格式
//        borderPage();

//         流式 布局格式
//        flowPage();

        // 表格 布局格式
//        gridPage();

        mixPage();
    }

    private static void borderPage() {
//        l  可以把组件放在这五个位置的任意一个，如果未指定位置，则缺省的位置是CENTER。
//        l  南、北位置控件各占据一行，控件宽度将自动布满整行。东、西和中间位置占据一行;若东、西、南、北位置无控件，则中间控件将自动布满整个屏幕。若东、西、南、北位置中无论哪个位置没有控件，则中间位置控件将自动占据没有控件的位置。
//        l  它是窗口、框架的内容窗格和对话框等的缺省布局。
        JFrame borderFrame = new JFrame("东西南北中布局");
        borderFrame.pack();  //让窗体自适应组建大小
        borderFrame.setVisible(true);
        borderFrame.setSize(100, 100);
        borderFrame.setLocation(300, 100);
        borderFrame.setResizable(true);//窗口大小不可变更
        borderFrame.setLayout(new BorderLayout());
        //设置监听事件，使得窗口可关闭
        borderFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //new五个button
        JButton east = new JButton("East");
        JButton west = new JButton("West");
        JButton south = new JButton("South");
        JButton north = new JButton("North");
        JButton center = new JButton("Center");

        //对center按钮添加监听事件
        center.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0 ;; i++) {
                    try {
                        west.setBackground(new Color(0xB81717));
                        Thread.sleep(500);
                        north.setBackground(new Color(0xD7A206));
                        Thread.sleep(500);
                        east.setBackground(new Color(0x35B817));
                        Thread.sleep(500);
                        south.setBackground(new Color(0x1785B8));
                        Thread.sleep(500);
                        center.setBackground(new Color(0xA817B8));
                        Thread.sleep(500);

                        west.setBackground(null);
                        north.setBackground(null);
                        east.setBackground(null);
                        south.setBackground(null);
                        center.setBackground(null);
                        Thread.sleep(500);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                }
            }
        });

        //将五个button加入到frame窗口中
        borderFrame.add(east, BorderLayout.EAST);
        borderFrame.add(west, BorderLayout.WEST);
        borderFrame.add(south, BorderLayout.SOUTH);
        borderFrame.add(north, BorderLayout.NORTH);
        borderFrame.add(center, BorderLayout.CENTER);
    }

    private static void flowPage() {
//        按照设置对齐方式从左向右排列，一行排满到下一行继续排列。
        JFrame flowFrame = new JFrame("流式布局格式");
        flowFrame.pack();  //让窗体自适应组建大小
        flowFrame.setVisible(true);
        flowFrame.setSize(100, 100);
        flowFrame.setLocation(300, 100);
        flowFrame.setResizable(true);//窗口大小不可变更
        flowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        flowFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
        for (int i = 1; i < 10; i ++) {
            String idx = Integer.toString(i * 111);
            flowFrame.getContentPane().add(new JButton(idx));
        }
    }

    private static void gridPage() {
//        它以矩形网格形式对容器的组件进行布置，把容器按行列分成大小相等的矩形网格
//        一个网格中放置一个组件，组件宽高自动撑满网格
        JFrame gridFrame = new JFrame("表格布局格式");
        gridFrame.pack();  //让窗体自适应组建大小
        gridFrame.setVisible(true);
        gridFrame.setSize(100, 100);
        gridFrame.setLocation(300, 100);
        gridFrame.setResizable(true);
        gridFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gridFrame.setLayout(new GridLayout(5,2));
        for (int i = 1; i < 11; i ++) {
            String idx = Integer.toString(i * 111);
            gridFrame.getContentPane().add(new JButton(idx));
        }
    }

    private static void mixPage() {
        JFrame mixFrame = new JFrame("组合面板");
        mixFrame.pack();
        mixFrame.setVisible(true);
        mixFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mixFrame.setSize(300,350);
        mixFrame.setLocation(450, 200);
        mixFrame.setLayout(new GridLayout(2, 1));

        JPanel upPanel = new JPanel(new BorderLayout());
        upPanel.add(new JButton("西"), BorderLayout.EAST);
        upPanel.add(new JButton("北"), BorderLayout.NORTH);
        upPanel.add(new JButton("东"), BorderLayout.WEST);
        upPanel.add(new JButton("南"), BorderLayout.SOUTH);

        JPanel numPanel = new JPanel(new FlowLayout());
        for (int i = 1; i < 10; i ++) {
            String idx = Integer.toString(i);
            numPanel.add(new JButton(idx));
        }
        numPanel.add(new JButton("*"));
        numPanel.add(new JButton("0"));
        numPanel.add(new JButton("#"));
        upPanel.add(numPanel, BorderLayout.CENTER);

        JPanel downPanel = new JPanel(new GridLayout(1,3));
        JTextArea ytext = new JTextArea("y");
        ytext.setBackground(new Color(0x8080FF));
        JTextArea y2text = new JTextArea("y");
        y2text.setBackground(new Color(0x8080FF));

        JTextArea btext = new JTextArea("b");
        btext.setBackground(new Color(0x8080FF));
        downPanel.add(ytext);
        downPanel.add(y2text);
        downPanel.add(btext);

        mixFrame.add(upPanel);
        mixFrame.add(downPanel);
    }
}
